﻿using bleissem.CMWVS.UI;
using CMWVS.Core;
using CMWVS.Core.Commands;
using CMWVS.Core.Mono;
using EnvDTE80;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace bleissem.CMWVS.Commands
{
    public class CleanCommand : BaseCommand
    {
        #region Constructor

        public CleanCommand()
            : base(new CommandID(GuidList.GuidCMWVS_Plugin_Clean, (int)PkgCmdIDList.IDCMWVS_Plugin_Clean))
        {
        }

        #endregion Constructor

        protected override void OnBeforeQueryStatus()
        {
            this.Enabled = base.Resolver.Get<MonoConfig>().IsInstalled;

            base.OnBeforeQueryStatus();
        }

        protected override void Execute()
        {
            var clean = base.m_MainViewModel.Clean;
            if (clean.CanExecute())
            {
                DTE2 dte2 = base.Resolver.Get<DTE2>();
                EnvDTE.Solution solution = dte2.Solution;

                if (null == solution)
                {
                    return;
                }

                ToOutputWindow outputWindow = new ToOutputWindow(base.Resolver);
                outputWindow.Clear();

                using (TraceListener traceListener = new AddInTraceListener((string message) =>
                {
                    outputWindow.OutPut(message);
                }))
                {
                    CleanCommandParameters compileParams;
                    BuildCompileParameters.Build(solution, traceListener, out compileParams);

                    clean.Execute(compileParams);
                    var result = clean.Result;
                }
            }
        }
    }
}